package server;

import common.Message;
import common.NowUser;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Polygon;
import javafx.scene.text.Font;
import view.controller.ChatController;
import view.controller.ManageChat;
import view.Chat;

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.util.concurrent.BrokenBarrierException;

public class ChatThread extends Thread{
    ObjectOutputStream oos = null;
    ObjectInputStream ois = null;
    public boolean last = true;
    public Socket socket;

    public ChatThread(Socket socket) throws IOException{
        this.socket = socket;
        oos = NowUser.oos;
        ois = NowUser.ois;
    }
    
    public Socket getSocket(){
        return socket;
    }
    public void setSocket(){
        this.socket = socket;
    }
    
    public void run(){
        while (true){
            try{
                Message msg = (Message)ois.readObject();
                System.out.println(msg.getType() + "  client");
                if(msg.getType().equals("closeac")){
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("getfriendac")) {
                    NowUser.setFriend(msg.getUser());
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("getuserac")) {
                    NowUser.setUser(msg.getUser());
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("getfriendsac")) {
                    NowUser.setFriendslist(msg.getFriendList());
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("getgroupsac")) {
                    NowUser.setGroupslist(msg.getGroupList());
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("getgroupac")) {
                    NowUser.setGroup(msg.getGroup());
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("getothergroupsac")) {
                    NowUser.setOthergroupslist(msg.getOwngroupList());
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("getinformationsac")) {
                    NowUser.setFriendinformationlist(msg.getIlist());
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("getgroupinformationsac")) {
                    NowUser.setGroupinformationlist(msg.getQilist());
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("checkuserac")) {
                    NowUser.setExistfriend(true);
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("eachotherac")) {
                    // System.out.println("互为好友");
                    NowUser.setFriendrelation(1);
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("eachotherf")) {
                    NowUser.setFriendrelation(2);
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("eachotherff")) {
                    NowUser.setFriendrelation(3);
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("addfriendac")) {
                    NowUser.addfriend = true;
                    NowUser.cyclicBarrier.await();
                }else if(msg.getType().equals("eachother")){
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("sendedac")) {
                    NowUser.grouprelation = 1;
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("belonggroupac")) {
                    NowUser.grouprelation = 2;
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("grouprelationac")) {
                    NowUser.grouprelation = 3;
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("nothisgroup")) {
                    NowUser.grouprelation = 4;
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("acceptfriendac")) {
                    NowUser.acfriendinfo = true;
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("acceptfalsefriendac")) {
                    NowUser.rjfriendinfo = true;
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("changeInformationac")) {
                    NowUser.changeinfomation = true;
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("xgcheckemailac")) {
                    NowUser.existemail = true;
                    NowUser.cyclicBarrier.await();
                }  else if (msg.getType().equals("xgpwdac")) {
                    NowUser.changepsw = true;
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("creatgroupac")) {
                    NowUser.creatgroup = true;
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("deletefriendac")) {
                    NowUser.deletefriend = true;
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("dissolutiongroupac")) {
                    NowUser.dissolutiongroup = true;
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("exitgroupac")) {
                    NowUser.exitgroup = true;
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("gapplytrueac")) {
                    NowUser.acceptgroup = true;
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("gapplyflaseac")) {
                    NowUser.refusegroup = true;
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("chatsendac")) {//私聊
                    if (!msg.getCommunication().getSender().getId().equals(NowUser.user.getId())) {
                        System.out.println(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                        Platform.runLater(() -> {
                            if (ManageChat.getChat(NowUser.user.getId() + msg.getCommunication().getSender().getId()) != null) {
                                //打开了聊天框
                                //获取到接收者这边打开的和发送者的聊天框
                                Chat chat = ManageChat.getChat(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                                ChatController chattingController = ManageChat.getchatcontroller(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                                System.out.println(chattingController);
                                FlowPane flowPane = ManageChat.getMeesagelist(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                                VBox.setVgrow(flowPane, Priority.ALWAYS);
                                URL url = null;
                                try {
                                    if (msg.getCommunication().getSender().getHeadimage() == null || msg.getCommunication().getSender().getHeadimage().equals("") || msg.getCommunication().getSender().getHeadimage().equals("null")) {
                                        url = new URL("file:E:\\untitled\\src\\view\\photo\\默认头像.jpg");
                                    } else {
                                        url = new URL("file:" + msg.getCommunication().getSender().getHeadimage());
                                    }
                                } catch (MalformedURLException e) {
                                    e.printStackTrace();
                                }
                                Image image = new Image(String.valueOf(url));
                                ImageView imageView = new ImageView(image);
                                imageView.setFitHeight(50);
                                imageView.setFitWidth(50);
                                Circle circle = new Circle();
                                circle.setCenterX(25);
                                circle.setCenterY(25);
                                circle.setRadius(25);
                                imageView.setClip(circle);
                                Label messageBubble = new Label(msg.getCommunication().getContent());
                                messageBubble.setWrapText(true);
                                messageBubble.setMaxWidth(220);
                                messageBubble.setMinWidth(25);
                                messageBubble.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
                                messageBubble.setPadding(new Insets(6));
                                messageBubble.setFont(new Font(14));
                                HBox.setMargin(messageBubble, new Insets(8, 0, 0, 0));

                                Label timeBubble = new Label(msg.getCommunication().getSendDateTime());
                                timeBubble.setWrapText(true);
                                timeBubble.setMaxWidth(180);
                                timeBubble.setMinWidth(25);
                                timeBubble.setStyle("-fx-background-color: transparent;");
                                HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));

                                double[] points;
                                points = new double[]{
                                        0.0, 5.0,
                                        10.0, 0.0,
                                        10.0, 10.0
                                };
                                Polygon triangle = new Polygon(points);
                                //设置颜色
                                triangle.setFill(Color.rgb(179, 231, 244));
                                HBox messageBox = new HBox();
                                messageBox.setPrefWidth(366);
                                //设置空隙
                                messageBox.setPadding(new Insets(10, 5, 10, 5));
                                HBox.setMargin(triangle, new Insets(15, 0, 0, 10));
                                messageBox.getChildren().addAll(imageView, triangle, messageBubble);
                                //last = scrollPane.getVvalue() == 1.0;
                                flowPane.getChildren().add(messageBox);
                                ScrollPane scrollPane = ManageChat.getScrollPaneHashMap(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                                //让发送消息显示在最底部
                                scrollPane.vvalueProperty().addListener(new ChangeListener<Number>() {
                                    @Override
                                    public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) {
                                        //判断滑轮是否在最下面
                                        if (last) {
                                            scrollPane.setVvalue(1.0);
                                            last = false;
                                        }
                                    }
                                });
                                scrollPane.setVvalue(1.0);
                                last = scrollPane.getVvalue() == 1.0;

                            } else {//没打开聊天框
                                System.out.println("没打开聊天框");
                            }
                        });
                        System.out.println("消息接收成功！" + msg.getCommunication().getContent());
                    } else {
                        NowUser.cyclicBarrier.await();
                    }
                } else if (msg.getType().equals("recordsac")) {//私聊聊天记录
                    NowUser.friendchatlist = msg.getFclist();
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("groupchatsendac")) {//群聊
                    System.out.println(msg.getCommunication().getSender().getId());
                    if (!msg.getCommunication().getSender().getId().equals(NowUser.user.getId())) {
                        // System.out.println(Common.user.getUserid()+msg.getCommunication().getSender().getUserid());
                        Platform.runLater(() -> {
                            if (ManageChat.getGroupMeesagelist(NowUser.user.getId() + msg.getCommunication().getGroup().getGroupid()) != null) {
                                //打开了聊天框
                                //获取到接收者这边打开的和发送者的聊天框
                                FlowPane flowPane = ManageChat.getGroupMeesagelist(NowUser.user.getId() + msg.getCommunication().getGroup().getGroupid());
                                VBox.setVgrow(flowPane, Priority.ALWAYS);
                                URL url = null;
                                try {
                                    if (msg.getCommunication().getSender().getHeadimage() == null || msg.getCommunication().getSender().getHeadimage().equals("") || msg.getCommunication().getSender().getHeadimage().equals("null")) {
                                        url = new URL("file:E:\\untitled\\src\\view\\photo\\默认头像.jpg");
                                    } else {
                                        url = new URL("file:" + msg.getCommunication().getSender().getHeadimage());
                                    }
                                } catch (MalformedURLException e) {
                                    e.printStackTrace();
                                }
                                Image image = new Image(String.valueOf(url));
                                ImageView imageView = new ImageView(image);
                                imageView.setFitHeight(50);
                                imageView.setFitWidth(50);
                                Circle circle = new Circle();
                                circle.setCenterX(25);
                                circle.setCenterY(25);
                                circle.setRadius(25);
                                imageView.setClip(circle);
                                Label messageBubble = new Label(msg.getCommunication().getContent());
                                messageBubble.setWrapText(true);
                                messageBubble.setMaxWidth(220);
                                messageBubble.setMinWidth(25);
                                messageBubble.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
                                messageBubble.setPadding(new Insets(6));
                                messageBubble.setFont(new Font(14));
                                HBox.setMargin(messageBubble, new Insets(8, 0, 0, 0));

                                Label timeBubble = new Label(msg.getCommunication().getSendDateTime());
                                timeBubble.setWrapText(true);
                                timeBubble.setMaxWidth(180);
                                timeBubble.setMinWidth(25);
                                timeBubble.setStyle("-fx-background-color: transparent;");
                                HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));

                                double[] points;
                                points = new double[]{
                                        0.0, 5.0,
                                        10.0, 0.0,
                                        10.0, 10.0
                                };
                                Polygon triangle = new Polygon(points);
                                //设置颜色
                                triangle.setFill(Color.rgb(179, 231, 244));
                                HBox messageBox = new HBox();
                                messageBox.setPrefWidth(366);
                                //设置空隙
                                messageBox.setPadding(new Insets(10, 5, 10, 5));
                                HBox.setMargin(triangle, new Insets(15, 0, 0, 10));
                                messageBox.getChildren().addAll(imageView, triangle, messageBubble);
                                //last = scrollPane.getVvalue() == 1.0;
                                flowPane.getChildren().add(messageBox);
                                ScrollPane scrollPane = ManageChat.getGroupScrollPaneHashMap(NowUser.user.getId() + msg.getCommunication().getGroup().getGroupid());
                                //让发送消息显示在最底部
                                scrollPane.vvalueProperty().addListener(new ChangeListener<Number>() {
                                    @Override
                                    public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) {
                                        //判断滑轮是否在最下面
                                        if (last) {
                                            scrollPane.setVvalue(1.0);
                                            last = false;
                                        }
                                    }
                                });
                                scrollPane.setVvalue(1.0);
                                last = scrollPane.getVvalue() == 1.0;
                            } else {//没打开聊天框
                                System.out.println("没打开聊天框");
                            }
                        });
                        System.out.println("消息接收成功！" + msg.getCommunication().getContent());
                    } else {
                        NowUser.cyclicBarrier.await();
                        System.out.println(msg.getCommunication().getSender().getId() + "sss");
                    }
                } else if (msg.getType().equals("grouprecordsac")) {//群聊聊天记录
                    NowUser.groupchatlist = msg.getQclist();
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("getmembersac")) {//查找群成员
                    NowUser.setGroupmemberstatement(msg.getQmslist());
                    NowUser.setGroupmembers(msg.getQmlist());
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("groupinformationac")) {//修改群聊信息(是否为群主)
                    NowUser.groupowner = true;
                    NowUser.group = msg.getGroup();
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("groupinformationwa")) {//修改群聊信息(是否为群主)
                    NowUser.groupowner = false;
                    NowUser.group = msg.getGroup();
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("changeGroupInformationac")) {//修改群资料
                    NowUser.changegroupinformation = true;
                    NowUser.cyclicBarrier.await();
                } else if (msg.getType().equals("sendphotoac")) {//私聊发图片
                    if (!msg.getCommunication().getSender().getId().equals(NowUser.user.getId())) {
                        System.out.println(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                        Platform.runLater(() -> {
                            if (ManageChat.getChat(NowUser.user.getId() + msg.getCommunication().getSender().getId()) != null) {
                                //打开了聊天框
                                //获取到接收者这边打开的和发送者的聊天框
                                Chat chat = ManageChat.getChat(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                                ChatController chattingController = ManageChat.getchatcontroller(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                                System.out.println(chattingController);
                                FlowPane flowPane = ManageChat.getMeesagelist(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                                VBox.setVgrow(flowPane, Priority.ALWAYS);
                                URL url = null;
                                try {
                                    if (msg.getCommunication().getSender().getHeadimage() == null || msg.getCommunication().getSender().getHeadimage().equals("") || msg.getCommunication().getSender().getHeadimage().equals("null")) {
                                        url = new URL("file:E:\\untitled\\src\\view\\photo\\默认头像.jpg");
                                    } else {
                                        url = new URL("file:" + msg.getCommunication().getSender().getHeadimage());
                                    }
                                } catch (MalformedURLException e) {
                                    e.printStackTrace();
                                }
                                System.out.println(url.toString());
                                Image image = new Image(String.valueOf(url));
                                ImageView imageView = new ImageView(image);
                                imageView.setFitHeight(50);
                                imageView.setFitWidth(50);
                                Circle circle = new Circle();
                                circle.setCenterX(25);
                                circle.setCenterY(25);
                                circle.setRadius(25);
                                imageView.setClip(circle);
                                Image imgpath = new Image("file:" + msg.getCommunication().getContent());
                                ImageView messageBubble = new ImageView(imgpath);
                                messageBubble.setFitHeight(220);
                                messageBubble.setFitWidth(220);
                                messageBubble.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
                                HBox.setMargin(messageBubble, new Insets(8, 0, 0, 0));

                                Label timeBubble = new Label(msg.getCommunication().getSendDateTime());
                                timeBubble.setWrapText(true);
                                timeBubble.setMaxWidth(180);
                                timeBubble.setMinWidth(25);
                                timeBubble.setStyle("-fx-background-color: transparent;");
                                HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));

                                double[] points;
                                points = new double[]{
                                        0.0, 5.0,
                                        10.0, 0.0,
                                        10.0, 10.0
                                };
                                Polygon triangle = new Polygon(points);
                                //设置颜色
                                triangle.setFill(Color.rgb(179, 231, 244));
                                HBox messageBox = new HBox();
                                messageBox.setPrefWidth(366);
                                //设置空隙
                                messageBox.setPadding(new Insets(10, 5, 10, 5));
                                HBox.setMargin(triangle, new Insets(15, 0, 0, 10));
                                messageBox.getChildren().addAll(imageView, triangle, messageBubble);
                                flowPane.getChildren().add(messageBox);
                                ScrollPane scrollPane = ManageChat.getScrollPaneHashMap(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                                //System.out.println(scrollPane+"hhh"+Common.user.getUserid()+msg.getCommunication().getSender().getUserid());
                                //让发送消息显示在最底部
                                scrollPane.vvalueProperty().addListener(new ChangeListener<Number>() {
                                    @Override
                                    public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) {
                                        //判断滑轮是否在最下面
                                        if (last) {
                                            scrollPane.setVvalue(1.0);
                                            last = false;
                                        }
                                    }
                                });
                                scrollPane.setVvalue(1.0);
                                last = scrollPane.getVvalue() == 1.0;
                            } else {//没打开聊天框
                                System.out.println("没打开聊天框");
                            }
                        });
                        System.out.println("消息接收成功！" + msg.getCommunication().getContent());
                    } else {
                        NowUser.communication = msg.getCommunication();
                        NowUser.cyclicBarrier.await();
                        System.out.println(msg.getCommunication().getSender().getId() + "sss");
                    }
                } else if (msg.getType().equals("sendfileac")) {//私聊发文件
                    if (!msg.getCommunication().getSender().getId().equals(NowUser.user.getId())) {
                        System.out.println(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                        Platform.runLater(() -> {
                            if (ManageChat.getChat(NowUser.user.getId() + msg.getCommunication().getSender().getId()) != null) {
                                //打开了聊天框
                                //获取到接收者这边打开的和发送者的聊天框
                                Chat chat = ManageChat.getChat(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                                ChatController chattingController = ManageChat.getchatcontroller(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                                System.out.println(chattingController);
                                FlowPane flowPane = ManageChat.getMeesagelist(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                                VBox.setVgrow(flowPane, Priority.ALWAYS);
                                URL url = null;
                                try {
                                    if (msg.getCommunication().getSender().getHeadimage() == null || msg.getCommunication().getSender().getHeadimage().equals("") || msg.getCommunication().getSender().getHeadimage().equals("null")) {
                                        url = new URL("file:E:\\untitled\\src\\view\\photo\\默认头像.jpg");
                                    } else {
                                        url = new URL("file:" + msg.getCommunication().getSender().getHeadimage());
                                    }
                                } catch (MalformedURLException e) {
                                    e.printStackTrace();
                                }
                                System.out.println(url.toString());
                                Image image = new Image(String.valueOf(url));
                                ImageView imageView = new ImageView(image);
                                imageView.setFitHeight(50);
                                imageView.setFitWidth(50);
                                Circle circle = new Circle();
                                circle.setCenterX(25);
                                circle.setCenterY(25);
                                circle.setRadius(25);
                                imageView.setClip(circle);
                                Image imgpath = new Image("file:" + "E:\\untitled\\src\\view\\photo\\标\\上传文件.png");
                                ImageView imgv = new ImageView(imgpath);
                                //System.out.println(communication.getContent()+"怕怕怕怕怕");
                                //换行
                                imgv.setFitHeight(220);
                                imgv.setFitWidth(220);
                                imgv.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
                                //imgv.setPadding(new Insets(6));
                                //imgv.setFont(new Font(14));
                                imgv.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
                                    @Override
                                    public void handle(MouseEvent mouseEvent) {
                                        File opfile = new File(msg.getCommunication().getContent());
                                        if (opfile.exists()) {
                                            Desktop desktop = Desktop.getDesktop();
                                            try {
                                                desktop.open(opfile);
                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    }
                                });
                                HBox.setMargin(imgv, new Insets(8, 0, 0, 0));

                                Label timeBubble = new Label(msg.getCommunication().getSendDateTime());
                                timeBubble.setWrapText(true);
                                timeBubble.setMaxWidth(180);
                                timeBubble.setMinWidth(25);
                                timeBubble.setStyle("-fx-background-color: transparent;");
                                HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));

                                double[] points;
                                points = new double[]{
                                        0.0, 5.0,
                                        10.0, 0.0,
                                        10.0, 10.0
                                };
                                Polygon triangle = new Polygon(points);
                                //设置颜色
                                triangle.setFill(Color.rgb(179, 231, 244));
                                HBox messageBox = new HBox();
                                messageBox.setPrefWidth(366);
                                //设置空隙
                                messageBox.setPadding(new Insets(10, 5, 10, 5));
                                HBox.setMargin(triangle, new Insets(15, 0, 0, 10));
                                messageBox.getChildren().addAll(imageView, triangle, imgv);
                                flowPane.getChildren().add(messageBox);
                                ScrollPane scrollPane = ManageChat.getScrollPaneHashMap(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                                //System.out.println(scrollPane+"hhh"+Common.user.getUserid()+msg.getCommunication().getSender().getUserid());
                                //让发送消息显示在最底部
                                scrollPane.vvalueProperty().addListener(new ChangeListener<Number>() {
                                    @Override
                                    public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) {
                                        //判断滑轮是否在最下面
                                        if (last) {
                                            scrollPane.setVvalue(1.0);
                                            last = false;
                                        }
                                    }
                                });
                                scrollPane.setVvalue(1.0);
                                last = scrollPane.getVvalue() == 1.0;

                            } else {//没打开聊天框
                                System.out.println("没打开聊天框");

                            }

                        });
                        System.out.println("消息接收成功！" + msg.getCommunication().getContent());
                    } else {
                        NowUser.communication = msg.getCommunication();
                        NowUser.cyclicBarrier.await();
                        System.out.println(msg.getCommunication().getSender().getId() + "sss");
                    }
                } else if (msg.getType().equals("sendgroupphotoac")) {//群图片
                    if (!msg.getCommunication().getSender().getId().equals(NowUser.user.getId())) {
                        System.out.println(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                        Platform.runLater(() -> {
                            if (ManageChat.getGroupMeesagelist(NowUser.user.getId() + msg.getCommunication().getGroup().getGroupid()) != null) {
                                //打开了聊天框
                                //获取到接收者这边打开的和发送者的聊天框
                                //Chatting chat=ManageChat.getChat(Common.user.getUserid()+msg.getCommunication().getSender().getUserid());
                                //ChattingController chattingController=ManageChat.getchatcontroller(Common.user.getUserid()+msg.getCommunication().getSender().getUserid());
                                //System.out.println(chattingController);
                                FlowPane flowPane = ManageChat.getGroupMeesagelist(NowUser.user.getId() + msg.getCommunication().getGroup().getGroupid());
                                VBox.setVgrow(flowPane, Priority.ALWAYS);
                                URL url = null;
                                try {
                                    if (msg.getCommunication().getSender().getHeadimage() == null || msg.getCommunication().getSender().getHeadimage().equals("") || msg.getCommunication().getSender().getHeadimage().equals("null")) {
                                        url = new URL("file:E:\\untitled\\src\\view\\photo\\默认头像.jpg");
                                    } else {
                                        url = new URL("file:" + msg.getCommunication().getSender().getHeadimage());
                                    }
                                } catch (MalformedURLException e) {
                                    e.printStackTrace();
                                }
                                System.out.println(url.toString());
                                Image image = new Image(String.valueOf(url));
                                ImageView imageView = new ImageView(image);
                                imageView.setFitHeight(50);
                                imageView.setFitWidth(50);
                                Circle circle = new Circle();
                                circle.setCenterX(25);
                                circle.setCenterY(25);
                                circle.setRadius(25);
                                imageView.setClip(circle);
                                Image imgpath = new Image("file:" + msg.getCommunication().getContent());
                                ImageView messageBubble = new ImageView(imgpath);
                                messageBubble.setFitHeight(220);
                                messageBubble.setFitWidth(220);
                                messageBubble.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
                                HBox.setMargin(messageBubble, new Insets(8, 0, 0, 0));

                                Label timeBubble = new Label(msg.getCommunication().getSendDateTime());
                                timeBubble.setWrapText(true);
                                timeBubble.setMaxWidth(180);
                                timeBubble.setMinWidth(25);
                                timeBubble.setStyle("-fx-background-color: transparent;");
                                HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));

                                double[] points;
                                points = new double[]{
                                        0.0, 5.0,
                                        10.0, 0.0,
                                        10.0, 10.0
                                };
                                Polygon triangle = new Polygon(points);
                                //设置颜色
                                triangle.setFill(Color.rgb(179, 231, 244));
                                HBox messageBox = new HBox();
                                messageBox.setPrefWidth(366);
                                //设置空隙
                                messageBox.setPadding(new Insets(10, 5, 10, 5));
                                HBox.setMargin(triangle, new Insets(15, 0, 0, 10));
                                messageBox.getChildren().addAll(imageView, triangle, messageBubble);
                                //last = scrollPane.getVvalue() == 1.0;
                                flowPane.getChildren().add(messageBox);
                                ScrollPane scrollPane = ManageChat.getGroupScrollPaneHashMap(NowUser.user.getId() + msg.getCommunication().getGroup().getGroupid());
                                //System.out.println(scrollPane+"hhh"+Common.user.getUserid()+msg.getCommunication().getSender().getUserid());
                                //让发送消息显示在最底部
                                scrollPane.vvalueProperty().addListener(new ChangeListener<Number>() {
                                    @Override
                                    public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) {
                                        //判断滑轮是否在最下面
                                        if (last) {
                                            scrollPane.setVvalue(1.0);
                                            last = false;
                                        }
                                    }
                                });
                                scrollPane.setVvalue(1.0);
                                last = scrollPane.getVvalue() == 1.0;
                            } else {//没打开聊天框
                                System.out.println("没打开聊天框");
                            }
                        });
                        System.out.println("消息接收成功！" + msg.getCommunication().getContent());
                    } else {
                        NowUser.communication = msg.getCommunication();
                        NowUser.cyclicBarrier.await();
                        System.out.println(msg.getCommunication().getSender().getId() + "sss");
                    }
                } else if (msg.getType().equals("sendgroupfileac")) {//群文件
                    if (!msg.getCommunication().getSender().getId().equals(NowUser.user.getId())) {
                        System.out.println(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                        Platform.runLater(() -> {
                            if (ManageChat.getGroupMeesagelist(NowUser.user.getId() + msg.getCommunication().getGroup().getGroupid()) != null) {
                                //打开了聊天框
                                //获取到接收者这边打开的和发送者的聊天框
                                //Chatting chat = ManageChat.getChat(Common.user.getUserid() + msg.getCommunication().getSender().getUserid());
                                //ChattingController chattingController = ManageChat.getchatcontroller(Common.user.getUserid() + msg.getCommunication().getSender().getUserid());
                                //System.out.println(chattingController);
                                FlowPane flowPane = ManageChat.getGroupMeesagelist(NowUser.user.getId() + msg.getCommunication().getGroup().getGroupid());
                                VBox.setVgrow(flowPane, Priority.ALWAYS);
                                URL url = null;
                                try {
                                    if (msg.getCommunication().getSender().getHeadimage() == null || msg.getCommunication().getSender().getHeadimage().equals("") || msg.getCommunication().getSender().getHeadimage().equals("null")) {
                                        url = new URL("file:E:\\untitled\\src\\view\\photo\\默认头像.jpg");
                                    } else {
                                        url = new URL("file:" + msg.getCommunication().getSender().getHeadimage());
                                    }
                                } catch (MalformedURLException e) {
                                    e.printStackTrace();
                                }
                                System.out.println(url.toString());
                                Image image = new Image(String.valueOf(url));
                                ImageView imageView = new ImageView(image);
                                imageView.setFitHeight(50);
                                imageView.setFitWidth(50);
                                Circle circle = new Circle();
                                circle.setCenterX(25);
                                circle.setCenterY(25);
                                circle.setRadius(25);
                                imageView.setClip(circle);
                                Image imgpath = new Image("file:" + "E:\\untitled\\src\\view\\photo\\标\\上传文件.png");
                                ImageView imgv = new ImageView(imgpath);
                                //System.out.println(communication.getContent()+"怕怕怕怕怕");
                                //换行
                                imgv.setFitHeight(220);
                                imgv.setFitWidth(220);
                                imgv.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
                                //imgv.setPadding(new Insets(6));
                                //imgv.setFont(new Font(14));
                                imgv.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
                                    @Override
                                    public void handle(MouseEvent mouseEvent) {
                                        File opfile = new File(msg.getCommunication().getContent());
                                        if (opfile.exists()) {
                                            Desktop desktop = Desktop.getDesktop();
                                            try {
                                                desktop.open(opfile);
                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    }
                                });
                                HBox.setMargin(imgv, new Insets(8, 0, 0, 0));

                                Label timeBubble = new Label(msg.getCommunication().getSendDateTime());
                                timeBubble.setWrapText(true);
                                timeBubble.setMaxWidth(180);
                                timeBubble.setMinWidth(25);
                                timeBubble.setStyle("-fx-background-color: transparent;");
                                HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));

                                double[] points;
                                points = new double[]{
                                        0.0, 5.0,
                                        10.0, 0.0,
                                        10.0, 10.0
                                };
                                Polygon triangle = new Polygon(points);
                                //设置颜色
                                triangle.setFill(Color.rgb(179, 231, 244));
                                HBox messageBox = new HBox();
                                messageBox.setPrefWidth(366);
                                //设置空隙
                                messageBox.setPadding(new Insets(10, 5, 10, 5));
                                HBox.setMargin(triangle, new Insets(15, 0, 0, 10));
                                messageBox.getChildren().addAll(imageView, triangle, imgv);
                                flowPane.getChildren().add(messageBox);
                                ScrollPane scrollPane = ManageChat.getGroupScrollPaneHashMap(NowUser.user.getId() + msg.getCommunication().getGroup().getGroupid());
                                //System.out.println(scrollPane + "hhh" + Common.user.getUserid() + msg.getCommunication().getSender().getUserid());
                                //让发送消息显示在最底部
                                scrollPane.vvalueProperty().addListener(new ChangeListener<Number>() {
                                    @Override
                                    public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) {
                                        //判断滑轮是否在最下面
                                        if (last) {
                                            scrollPane.setVvalue(1.0);
                                            last = false;
                                        }
                                    }
                                });
                                scrollPane.setVvalue(1.0);
                                last = scrollPane.getVvalue() == 1.0;
                            } else {//没打开聊天框
                                System.out.println("没打开聊天框");
                            }
                        });
                        System.out.println("消息接收成功！" + msg.getCommunication().getContent());
                    } else {
                        NowUser.communication = msg.getCommunication();
                        NowUser.cyclicBarrier.await();
                        System.out.println(msg.getCommunication().getSender().getId() + "sss");
                    }
                } else if (msg.getType().equals("groupemojiac")) {//群聊表情
                    if (!msg.getCommunication().getSender().getId().equals(NowUser.user.getId())) {
                        System.out.println(NowUser.user.getId() + msg.getCommunication().getGroup().getGroupid());
                        Platform.runLater(() -> {
                            if (ManageChat.getGroupMeesagelist(NowUser.user.getId() + msg.getCommunication().getGroup().getGroupid()) != null) {
                                //打开了聊天框
                                //获取到接收者这边打开的和发送者的聊天框
                                FlowPane flowPane = ManageChat.getGroupMeesagelist(NowUser.user.getId() + msg.getCommunication().getGroup().getGroupid());
                                VBox.setVgrow(flowPane, Priority.ALWAYS);
                                URL url = null;
                                try {
                                    if (msg.getCommunication().getSender().getHeadimage() == null || msg.getCommunication().getSender().getHeadimage().equals("") || msg.getCommunication().getSender().getHeadimage().equals("null")) {
                                        url = new URL("file:E:\\untitled\\src\\view\\photo\\默认头像.jpg");
                                    } else {
                                        url = new URL("file:" + msg.getCommunication().getSender().getHeadimage());
                                    }
                                } catch (MalformedURLException e) {
                                    e.printStackTrace();
                                }
                                System.out.println(url.toString());
                                Image image = new Image(String.valueOf(url));
                                ImageView imageView = new ImageView(image);
                                imageView.setFitHeight(50);
                                imageView.setFitWidth(50);
                                Circle circle = new Circle();
                                circle.setCenterX(25);
                                circle.setCenterY(25);
                                circle.setRadius(25);
                                imageView.setClip(circle);
                                Image imgpath = new Image("file:" + msg.getCommunication().getContent());
                                ImageView messageBubble = new ImageView(imgpath);
                                messageBubble.setFitHeight(35);
                                messageBubble.setFitWidth(35);
                                messageBubble.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
                                HBox.setMargin(messageBubble, new Insets(8, 0, 0, 0));

                                Label timeBubble = new Label(msg.getCommunication().getSendDateTime());
                                timeBubble.setWrapText(true);
                                timeBubble.setMaxWidth(180);
                                timeBubble.setMinWidth(25);
                                timeBubble.setStyle("-fx-background-color: transparent;");
                                HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));

                                double[] points;
                                points = new double[]{
                                        0.0, 5.0,
                                        10.0, 0.0,
                                        10.0, 10.0
                                };
                                Polygon triangle = new Polygon(points);
                                //设置颜色
                                triangle.setFill(Color.rgb(179, 231, 244));
                                HBox messageBox = new HBox();
                                messageBox.setPrefWidth(366);
                                //设置空隙
                                messageBox.setPadding(new Insets(10, 5, 10, 5));
                                HBox.setMargin(triangle, new Insets(15, 0, 0, 10));
                                messageBox.getChildren().addAll(imageView, triangle, messageBubble);
                                flowPane.getChildren().add(messageBox);
                                ScrollPane scrollPane = ManageChat.getGroupScrollPaneHashMap(NowUser.user.getId() + msg.getCommunication().getGroup().getGroupid());
                                //让发送消息显示在最底部
                                scrollPane.vvalueProperty().addListener(new ChangeListener<Number>() {
                                    @Override
                                    public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) {
                                        //判断滑轮是否在最下面
                                        if (last) {
                                            scrollPane.setVvalue(1.0);
                                            last = false;
                                        }
                                    }
                                });
                                scrollPane.setVvalue(1.0);
                                last = scrollPane.getVvalue() == 1.0;
                            } else {//没打开聊天框
                                System.out.println("没打开聊天框");
                            }
                        });
                        System.out.println("消息接收成功！" + msg.getCommunication().getContent());
                    }else{//转发给自己
                        Platform.runLater(() -> {
                            FlowPane flowPane=ManageChat.getGroupMeesagelist(NowUser.user.getId()+msg.getCommunication().getGroup().getGroupid());
                            ScrollPane scrollPane=ManageChat.getGroupScrollPaneHashMap(NowUser.user.getId()+msg.getCommunication().getGroup().getGroupid());
                            VBox.setVgrow(flowPane, Priority.ALWAYS);
                            URL url = null;
                            try {
                                if (msg.getCommunication().getSender().getHeadimage() == null || msg.getCommunication().getSender().getHeadimage().equals("") || msg.getCommunication().getSender().getHeadimage().equals("null")) {
                                    url = new URL("file:E:\\untitled\\src\\view\\photo\\默认头像.jpg");
                                } else {
                                    url = new URL("file:" + msg.getCommunication().getSender().getHeadimage());
                                }
                            } catch (MalformedURLException e) {
                                e.printStackTrace();
                            }
                            System.out.println(url.toString());
                            Image image = new Image(String.valueOf(url));
                            ImageView imageView = new ImageView(image);
                            imageView.setFitHeight(50);
                            imageView.setFitWidth(50);
                            Circle circle = new Circle();
                            circle.setCenterX(25);
                            circle.setCenterY(25);
                            circle.setRadius(25);
                            imageView.setClip(circle);
                            Image imgpath = new Image("file:" + msg.getCommunication().getContent());
                            ImageView messageBubble = new ImageView(imgpath);
                            messageBubble.setFitHeight(35);
                            messageBubble.setFitWidth(35);
                            messageBubble.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
                            HBox.setMargin(messageBubble, new Insets(8, 0, 0, 0));

                            //System.out.println(communication.getSendDateTime()+"可以");
                            Label timeBubble = new Label(msg.getCommunication().getSendDateTime());
                            timeBubble.setWrapText(true);
                            timeBubble.setMaxWidth(180);
                            timeBubble.setMinWidth(25);
                            timeBubble.setStyle("-fx-background-color: transparent;");
                            HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));

                            double[] points;
                            points = new double[]{
                                    0.0, 0.0,
                                    0.0, 10.0,
                                    10.0, 5.0
                            };
                            Polygon triangle = new Polygon(points);
                            //设置颜色
                            triangle.setFill(Color.rgb(179, 231, 244));
                            HBox messageBox = new HBox();
                            messageBox.setPrefWidth(514);
                            //设置空隙
                            messageBox.setPadding(new Insets(10, 5, 10, 5));
                            HBox.setMargin(triangle, new Insets(15, 10, 0, 0));
                            messageBox.getChildren().addAll(messageBubble, triangle, imageView);
                            flowPane.getChildren().add(messageBox);
                            messageBox.setAlignment(Pos.TOP_RIGHT);
                            //ScrollPane scrollPane = ManageChat.getGroupScrollPaneHashMap(Common.user.getUserid() + msg.getCommunication().getGrouper().getGroupid());
                            //让发送消息显示在最底部
                            scrollPane.vvalueProperty().addListener(new ChangeListener<Number>() {
                                @Override
                                public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) {
                                    //判断滑轮是否在最下面
                                    if (last) {
                                        scrollPane.setVvalue(1.0);
                                        last = false;
                                    }
                                }
                            });
                            scrollPane.setVvalue(1.0);
                            last = scrollPane.getVvalue() == 1.0;
                        });
                        NowUser.cyclicBarrier.await();
                    }
                }else if (msg.getType().equals("emojiac")) {
                    System.out.println(msg.getCommunication().getSender().getId()+"sb");
                    if (!msg.getCommunication().getSender().getId().equals(NowUser.user.getId())) {
                        System.out.println(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                        Platform.runLater(() -> {
                            if (ManageChat.getMeesagelist(NowUser.user.getId() + msg.getCommunication().getSender().getId()) != null) {
                                //打开了聊天框
                                //获取到接收者这边打开的和发送者的聊天框
                                FlowPane flowPane = ManageChat.getMeesagelist(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                                VBox.setVgrow(flowPane, Priority.ALWAYS);
                                URL url = null;
                                try {
                                    if (msg.getCommunication().getSender().getHeadimage() == null || msg.getCommunication().getSender().getHeadimage().equals("") || msg.getCommunication().getSender().getHeadimage().equals("null")) {
                                        url = new URL("file:E:\\untitled\\src\\view\\photot\\默认头像.jpg");
                                    } else {
                                        url = new URL("file:" + msg.getCommunication().getSender().getHeadimage());
                                    }
                                } catch (MalformedURLException e) {
                                    e.printStackTrace();
                                }
                                System.out.println(url.toString());
                                Image image = new Image(String.valueOf(url));
                                ImageView imageView = new ImageView(image);
                                imageView.setFitHeight(50);
                                imageView.setFitWidth(50);
                                Circle circle = new Circle();
                                circle.setCenterX(25);
                                circle.setCenterY(25);
                                circle.setRadius(25);
                                imageView.setClip(circle);
                                Image imgpath = new Image("file:" + msg.getCommunication().getContent());
                                ImageView messageBubble = new ImageView(imgpath);
                                messageBubble.setFitHeight(35);
                                messageBubble.setFitWidth(35);
                                messageBubble.setStyle("-fx-background-color: rgb(179,231,244); -fx-background-radius: 8px;");
                                HBox.setMargin(messageBubble, new Insets(8, 0, 0, 0));

                                Label timeBubble = new Label(msg.getCommunication().getSendDateTime());
                                timeBubble.setWrapText(true);
                                timeBubble.setMaxWidth(180);
                                timeBubble.setMinWidth(25);
                                timeBubble.setStyle("-fx-background-color: transparent;");
                                HBox.setMargin(timeBubble, new Insets(8, 0, 0, 0));

                                double[] points;
                                points = new double[]{
                                        0.0, 5.0,
                                        10.0, 0.0,
                                        10.0, 10.0
                                };
                                Polygon triangle = new Polygon(points);
                                //设置颜色
                                triangle.setFill(Color.rgb(179, 231, 244));
                                HBox messageBox = new HBox();
                                messageBox.setPrefWidth(366);
                                //设置空隙
                                messageBox.setPadding(new Insets(10, 5, 10, 5));
                                HBox.setMargin(triangle, new Insets(15, 0, 0, 10));
                                messageBox.getChildren().addAll(imageView, triangle, messageBubble);
                                flowPane.getChildren().add(messageBox);
                                ScrollPane scrollPane = ManageChat.getScrollPaneHashMap(NowUser.user.getId() + msg.getCommunication().getSender().getId());
                                //让发送消息显示在最底部
                                scrollPane.vvalueProperty().addListener(new ChangeListener<Number>() {
                                    @Override
                                    public void changed(ObservableValue<? extends Number> observableValue, Number oldValue, Number newValue) {
                                        //判断滑轮是否在最下面
                                        if (last) {
                                            scrollPane.setVvalue(1.0);
                                            last = false;
                                        }
                                    }
                                });
                                scrollPane.setVvalue(1.0);
                                last = scrollPane.getVvalue() == 1.0;
                            } else {//没打开聊天框
                                System.out.println("没打开聊天框");
                            }
                        });
                        System.out.println("消息接收成功！" + msg.getCommunication().getContent());
                    }else{//转发给自己
                        System.out.println(msg.getCommunication().getReceiver().getId()+"hhh");
                        NowUser.cyclicBarrier.await();
                    }
                }
                System.out.println("client处理完成");
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (BrokenBarrierException e) {
                e.printStackTrace();
            }
        }
    }
}
